package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.AgencyGrantRecord;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VAgencyGrantRecord;
import com.jxc.service.AgencyServiceI;
@Service("agencyService")
public class AgencyServiceImpl implements AgencyServiceI {

	@Autowired
	private BaseDaoI<AgencyGrantRecord> agencyDao;

	@Override
	public DataGrid datagrid(VAgencyGrantRecord vAgencyGrantRecord) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vAgencyGrantRecord)));
		dg.setTotal(this.total(vAgencyGrantRecord));
		return dg;
	}

	private List<VAgencyGrantRecord> changeModel(List<AgencyGrantRecord> agencys) {
		List<VAgencyGrantRecord> vagencys = new ArrayList<VAgencyGrantRecord>();
		if (agencys != null && agencys.size() > 0) {
			for (AgencyGrantRecord agency : agencys) {
				VAgencyGrantRecord vagency = new VAgencyGrantRecord();
				BeanUtils.copyProperties(agency, vagency);

				vagency.setAid(agency.getAgency().getId());
				vagency.setAusername(agency.getAgency().getUsername());
				vagency.setArealName(agency.getAgency().getRealName());

				vagency.setDid(agency.getDealer().getId());
				vagency.setDusername(agency.getDealer().getUsername());

				vagencys.add(vagency);
			}
		}
		return vagencys;
	}

	private List<AgencyGrantRecord> find(VAgencyGrantRecord vAgencyGrantRecord) {
		String hql = "from AgencyGrantRecord t where t.dealer.id=? ";

		List<Object> values = new ArrayList<Object>();
		values.add(vAgencyGrantRecord.getDid());
		if (vAgencyGrantRecord.getSort() != null
				&& vAgencyGrantRecord.getOrder() != null) {
			hql += " order by " + vAgencyGrantRecord.getSort() + " "
					+ vAgencyGrantRecord.getOrder();
		}
		return agencyDao.find(hql, values, vAgencyGrantRecord.getPage(),
				vAgencyGrantRecord.getRows());
	}

	private Long total(VAgencyGrantRecord vAgencyGrantRecord) {
		String hql = "select count(*) from AgencyGrantRecord t where t.dealer.id=? ";
		List<Object> values = new ArrayList<Object>();
		values.add(vAgencyGrantRecord.getDid());
		return agencyDao.count(hql, values);
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					AgencyGrantRecord agency = agencyDao.get(
							AgencyGrantRecord.class, Long.parseLong(id.trim()));
					if (agency != null) {
						agencyDao.delete(agency);
					}
				}
			}
		}

	}

	@Override
	public Integer save(VAgencyGrantRecord vAgencyGrantRecord) {
		Integer result = 0;
		String hql = "from AgencyGrantRecord t where t.agency.id=? and t.dealer.id=? ";

		List<Object> values = new ArrayList<Object>();
		values.add(vAgencyGrantRecord.getAid());
		values.add(vAgencyGrantRecord.getDid());
		List<AgencyGrantRecord> list = agencyDao.find(hql, values, 1, 1);
		if (list != null && list.size() > 0) {
			result = 1;
		}
		AgencyGrantRecord agencyGrantRecord = new AgencyGrantRecord();
		User agency = new User();
		agency.setId(vAgencyGrantRecord.getAid());
		User dealer = new User();
		dealer.setId(vAgencyGrantRecord.getDid());
		agencyGrantRecord.setAgency(agency);
		agencyGrantRecord.setDealer(dealer);
		agencyDao.save(agencyGrantRecord);
		return result;

	}

}
